package com.hundsun.fcp.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.hundsun.fcp.R;
import com.hundsun.fcp.adapter.MineListViewAdapter;
import com.hundsun.fcp.base.BaseFragment;
import com.hundsun.fcp.bean.MineBean;
import com.hundsun.fcp.bean.MineBean.MineItem;
import com.hundsun.fcp.common.GotoTradingHelper;
import com.hundsun.fcp.common.Redirect;
import com.hundsun.fcp.common.utils.DisplayImageOptionsUtil;
import com.hundsun.fcp.common.utils.ViewUtils;
import com.hundsun.fcp.dao.MineDao;
import com.hundsun.fcp.manager.FcpUserManager;
import com.hundsun.fcp.openapi.trade.bean.ResponseLoginBean;
import com.hundsun.fcp.widget.ListViewForScrollView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.analytics.MobclickAgent;

public class MineFragments extends BaseFragment implements OnClickListener{
	
	private View mRootView ;
	/**用户头像*/
	private ImageView mUserHeaderIv ;
	private View mUserInfoView ;
	/**用户名*/
	private TextView mUserNameTv ;
	/**用户手机号*/
	private TextView mUserMobileTv ;
	/**用户个人项*/
	private ListViewForScrollView mUserItemListView ;
	/**系统设置项*/
	private ListViewForScrollView mSystemItemListView ;
	/**登陆/注册按钮*/
	private Button mTitleLoginBtn ;
	/**消息中心*/
	private View mMsgCenter;
	/**未读消息数*/
	private TextView mMsgCountTv ; 
	/**用户个人项适配器*/
	private MineListViewAdapter mUserAdapter  ;
	/**系统设置项适配器*/
	private MineListViewAdapter mSysteAdapter ;
	
	private MineDao mineDao ;
	private ResponseLoginBean mLoginResult;
	private String mClientNo;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		if(mRootView==null){
			mRootView = inflater.inflate(R.layout.fragment_mine, container, false) ;
			mineDao = new MineDao(getActivity()) ;
			initView();
			initListener();
			
		}else{
			ViewGroup parent = (ViewGroup) mRootView.getParent();
            if (null != parent) {
                parent.removeView(mRootView);
            }
		}
		
		return mRootView;
	}
	
	private void initListener() {
		mUserItemListView.setOnItemClickListener(userItemClick) ;	
		mSystemItemListView.setOnItemClickListener(systemItemClick) ;
		mTitleLoginBtn.setOnClickListener(mLoginClick) ;
		
	}
	//登陆操作
	private OnClickListener mLoginClick = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Redirect.startLogin(getActivity()) ;				
		}
	};
	//选择用户个人项
	private OnItemClickListener userItemClick = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			MineItem item = mUserAdapter.getItem(position) ;
			//判断用户是否已登录
			if(!FcpUserManager.getInstance().hasLoginUser()){
				Redirect.startLogin(getActivity()) ;
				return ;
			}
			switch (item.mType) {
			//账户安全
			case USERACCOUNT:
				Redirect.startAccSecurity(getActivity()) ;
				break;
			//我的银行卡	
			case USERBANKCARD: 
				Redirect.startBankManager(getActivity()) ;
				break ;

			default:
				break;
			}
		}
	};
	//选择系统设置项
	private OnItemClickListener systemItemClick = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			MineItem item = mSysteAdapter.getItem(position) ;
			switch (item.mType) {
			//活动中心
			case SYSTEMACTION:
				Redirect.startMineActionCenter(getActivity()) ;
				break;
			//帮助中心	
			case SYSTEMHELPING:
				GotoTradingHelper.doHelp(getActivity()) ;
				break;
			//意见反馈
			case SYSTEMFEEDBACK: 
				GotoTradingHelper.doFeedBack(getActivity()) ;
				break ;
			//关于我们
			case SYSTEMABOUT:
				GotoTradingHelper.doAbout(getActivity()) ;
				break ;
			//推荐给好友
			case SYSTEMSHARE:
				Redirect.startShareActivity(getActivity());
				break ;  
			//版本更新	
			case SYSTEMUPDATE:
				break;
				
			default:
				break;
			}
		}
	};
	
	
	private void initView() {
		mUserInfoView = mRootView.findViewById(R.id.rl_userinfo) ;
		mUserHeaderIv = (ImageView) mRootView.findViewById(R.id.iv_user_header) ;
		mUserNameTv = (TextView) mRootView.findViewById(R.id.tv_user_name) ;
		mUserMobileTv = (TextView) mRootView.findViewById(R.id.tv_user_mobile) ;
		mUserItemListView = (ListViewForScrollView) mRootView.findViewById(R.id.lv_useritem) ;
		mSystemItemListView = (ListViewForScrollView) mRootView.findViewById(R.id.lv_systemitem) ;
		mTitleLoginBtn = (Button) mRootView.findViewById(R.id.btn_login) ;
		mMsgCenter = mRootView.findViewById(R.id.rl_msg_center);
		mMsgCountTv = (TextView) mRootView.findViewById(R.id.tv_mine_msg_count) ;
		
		mUserAdapter = new MineListViewAdapter(getActivity()) ;
		mSysteAdapter = new MineListViewAdapter(getActivity()) ;
		
		mUserAdapter.setData(MineBean.userItems()) ;
		mSysteAdapter.setData(MineBean.systemItems()) ;
		mUserItemListView.setAdapter(mUserAdapter) ;
		mSystemItemListView.setAdapter(mSysteAdapter);
		
		mMsgCenter.setOnClickListener(this);
		mUserInfoView.setOnClickListener(this);
	
	}
	
	@Override
	public void onClick(View v) {
		if(v == mUserInfoView){
			Redirect.startAccInfo(getActivity());
		}else if(v == mMsgCenter){
			Redirect.startMsgCenterActivity(getActivity());
		}
	}
	public void onResume() {
		super.onResume();
		MobclickAgent.onPageStart(this.getClass().getSimpleName()) ;
		init();
	}
	
	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd(this.getClass().getSimpleName()) ;
	}

	private void init() { 
		initValue();
	}
	 

	private void initValue() {
		mClientNo = FcpUserManager.getInstance().getUserClientNo() ;
		if(mClientNo == null){ 
			mUserInfoView.setVisibility(View.GONE) ; 
			mTitleLoginBtn.setVisibility(View.VISIBLE) ;
			//MineBean.setUserItemDesc("--张", MineItemType.USERBANK) ;
		}else{
			mUserInfoView.setVisibility(View.VISIBLE); 
			mLoginResult = mineDao.queryMineResult(mClientNo) ;
			ViewUtils.setText(mUserNameTv, mLoginResult.getShortname());
			ViewUtils.setText(mUserMobileTv, mLoginResult.getMobile());
			mTitleLoginBtn.setVisibility(View.GONE) ;
			
		}
		mUserAdapter.notifyDataSetChanged() ;
		ImageLoader.getInstance().displayImage(null, mUserHeaderIv, mClientNo == null?
				DisplayImageOptionsUtil.unloginImageOptions():DisplayImageOptionsUtil.loginImageOptions()) ;
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		if(mRootView!=null){
			((ViewGroup)mRootView.getParent()).removeView(mRootView) ;
		}
	}

}
